//
//  MYGScheduleViewController.swift
//  MYGrow
//
//  Created by Mo on 2024/3/12.
//

import UIKit
import JXSegmentedView
class MYGScheduleViewController: MYGBaseController {
    var meetingModel:MYGScheduleMeetingNumModel = MYGScheduleMeetingNumModel()
//    var requested:Bool = false
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        initUI()
//        NotificationCenter.default.addObserver(self, selector: #selector(getAllSchedules), name: NSNotification.Name.init("requestMeetingsNum"), object: nil)

    }

    @objc func getAllSchedules(){
//        if requested == true{
//            return
//        }
//        requested = true
        MYGNetwork<MYGScheduleAPI, MYGScheduleMeetingNumModel>.request(target: .meetingsSchedule(params: ["doctor_id":MYGUserManager.user?.doctor.id ?? 0])) {[weak self] result in
            self?.meetingModel = result
            self?.handleMeetings()
            MYGLoading.hide()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    private func handleMeetings(){
        if meetingModel.meetingCount > 0 || meetingModel.taskCount > 0{
            let alertController = UIAlertController(title: "会议确认",
                                                    message: "识别到您有\(meetingModel.meetingCount)场会议，\(meetingModel.taskCount)个任务，请您确认是否前往查看", preferredStyle: .alert)
            let okAction3 = UIAlertAction(title: "取消", style: .default, handler: {
                action in
            })
            let redColor = UIColor.gray
            okAction3.setValue(redColor, forKey: "titleTextColor")
            let okAction4 = UIAlertAction(title: "前往查看", style: .default, handler: {[weak self]
                action in
                let vc = MYGMeetingsViewController()
                vc.meetings = self?.meetingModel.meeting_list ?? []
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            })
            alertController.addAction(okAction3)
            alertController.addAction(okAction4)
            self.present(alertController, animated: true)
        }
    }
    func initUI(){
        self.title = "我的日程"
        view.addSubview(headImageV)
        headImageV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(statusBarHeight + 44)
            make.top.equalToSuperview()
        }
        view.addSubview(titleLab)
        titleLab.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(statusBarHeight)
            make.height.equalTo(44)
        }
        view.addSubviews([addBtn,searchBtn])
        addBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14.5)
            make.centerY.equalTo(titleLab)
            make.size.equalTo(CGSize(width: 19, height: 19))
        }
        searchBtn.snp.makeConstraints { make in
            make.right.equalTo(addBtn.snp.left).offset(-7.5)
            make.centerY.equalTo(titleLab)
            make.size.equalTo(CGSize(width: 22, height: 22))
        }
        view.addSubview(segmentedView)
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(44))
            make.top.equalToSuperview().offset(statusBarHeight + 44)
        }
        //        changeBtn.snp.makeConstraints { make in
        //            make.right.equalToSuperview().offset(-17.5)
        //            make.centerY.equalTo(segmentedView)
        //            make.size.equalTo(CGSize(width: 16, height: 16))
        //        }
        view.addSubview(listContainerView)
        listContainerView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(statusBarHeight + 44 + SCALEWIDTH(44))
        }
        //关联listContainer
        segmentedView.listContainer = listContainerView
        view.addSubview(bigAddBtn)
        bigAddBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-2.5)
            make.bottom.equalToSuperview().offset(-bottomSafeheight - tabbarHeight - 60)
            make.width.height.equalTo(72)
        }
    }
    private lazy var headImageV:UIImageView = {
        let cont = UIImageView()
        cont.image = UIImage(named: "schedule_head")
        return cont
    }()
    lazy var addBtn: UIButton = {
        let control = UIButton(type: .custom)
        control.tintColor = .white
        control.setImage(UIImage(named: "addSchedule_refresh"), for: .normal)
        control.addTarget(self, action: #selector(getAllSchedules), for: .touchUpInside)
        return control
    }()
    lazy var searchBtn: UIButton = {
        let control = UIButton(type: .custom)
        control.tintColor = .white
        control.setImage(UIImage(named: "addSchedule_searchSche"), for: .normal)
        control.addTarget(self, action: #selector(searchSchedule), for: .touchUpInside)
        return control
    }()
    lazy var changeBtn: UIButton = {
        let control = UIButton(type: .custom)
        control.tintColor = .white
        control.setImage(UIImage(named: "addSchedule_exchangeSchedule"), for: .normal)
        control.addTarget(self, action: #selector(searchSchedule), for: .touchUpInside)
        return control
    }()
    lazy var titleLab:UILabel = {
        let cont = UILabel()
        cont.textColor = .rgba(255, 255, 255)
        cont.font = .medium(18)
        cont.text = "我的日程"
        cont.textAlignment = .center
        return cont
    }()
    @objc private func addSchedule(){
        self.navigationController?.pushViewController(MYGAddScheduleViewController())
    }
    @objc private func searchSchedule(){
        self.navigationController?.pushViewController(MYGScheduleSearchViewController())
    }
    @objc private func exchangeSchedule(){
        
    }
    private lazy var listContainerView: JXSegmentedListContainerView = {
        let vi = JXSegmentedListContainerView(dataSource: self)
        vi.scrollView.isScrollEnabled = false
        return vi
    }()
    private lazy var segmentedView: JXSegmentedView = {
        let segmentedView = JXSegmentedView()
        segmentedView.delegate = self
        segmentedView.dataSource = dataSource
        segmentedView.backgroundColor = .clear
        segmentedView.collectionView.backgroundColor = .clear
        segmentedView.contentScrollView?.isScrollEnabled = false
        return segmentedView
    }()
    private lazy var bigAddBtn:UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "scheduleAdd"), for: .normal)
        cont.addTarget(self, action: #selector(addSchedule), for: .touchUpInside)
        return cont
    }()
    private lazy var dataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.titles = ["全部日程", "今日日程"]
        dataSource.titleNormalFont = .regular(15)
        dataSource.titleNormalColor = .rgba(118, 118, 118, 1)
        dataSource.titleSelectedFont = .medium(18)
        dataSource.titleSelectedColor = .rgba(51, 51, 51, 1)
        dataSource.isItemSpacingAverageEnabled = false
        dataSource.itemSpacing = SCALEWIDTH(12)
        dataSource.itemWidthIncrement = SCALEWIDTH(12)
        return dataSource
    }()
}
extension MYGScheduleViewController: JXSegmentedViewDelegate{
    //点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，而不关心具体是点击还是滚动选中的情况。
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {}
    // 点击选中的情况才会调用该方法
    func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {}
    // 滚动选中的情况才会调用该方法
    func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {}
    // 正在滚动中的回调
    func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {}
    
}
extension MYGScheduleViewController:JXSegmentedListContainerViewDataSource{
    //返回列表的数量
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return 2
    }
    //返回遵从`JXSegmentedListContainerViewListDelegate`协议的实例
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0{
            return MYGAllIncientViewController()
        }else{
            return MYGCalendarViewController()
        }
    }
    
}
